# coding=utf-8
#!/usr/bin/python
# 导入requests库
import requests

# 导入文件操作库
import os
import bs4
from bs4 import BeautifulSoup
import sys
import importlib
import random
import time




# 越多越好
meizi_headers = [
    "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36",
    "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/537.75.14",
    "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)",
    "Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11",
    "Opera/9.25 (Windows NT 5.1; U; en)",
    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
    "Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Kubuntu)",
    "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12",
    "Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9",
    "Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.7 (KHTML, like Gecko) Ubuntu/11.04 Chromium/16.0.912.77 Chrome/16.0.912.77 Safari/535.7",
    "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:10.0) Gecko/20100101 Firefox/10.0",
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36",
]

# 给请求指定一个请求头来模拟chrome浏览器
global g_headers
g_headers = {"User-Agent": random.choice(meizi_headers)}
# 定义存储位置
global g_save_path
# g_save_path =  '/home/liubenlong/filebrowser/sites/root/photo1/'
g_save_path = "./imges/"

#抓包url
g_url_prfix = 'https://pic.netbian.com/'
g_url_begin = '%s4kmeinv/'%g_url_prfix

g_count = 0

def try_check(func):
    def in_func(*args, **kvargs):
        try:
            func(*args, **kvargs)
        except Exception as err:
            print(err)

    return in_func

# 创建文件夹
@try_check
def createDir(file_path):
    if os.path.exists(file_path) is False:
        os.makedirs(file_path)

# 下载图片
@try_check
def download(url1, name, index):
    global g_count
    g_count = g_count + 1
    
    r = requests.get(url1, timeout=100)
    dst_dir = '%s%s/'%(g_save_path, index)
    createDir(dst_dir)
    
    dst_path = '%s%d.jpg'%(dst_dir, g_count)
    print(dst_path)
    with open(dst_path, 'wb') as f:
        f.write(r.content)

# 图片查找
@try_check
def imagespider(url, index):
    global g_headers
    urls = []
    r = requests.get(url, headers=g_headers)
    #由于某些网站编码不一定是utf-8，需要译码
    r.content.decode('GBK')
    r.encoding = 'utf-8'
    soup = BeautifulSoup(r.content, 'html.parser', from_encoding='GBK')
    lis = soup.select("li a img")
    for li in lis:
        img = li['src']
        name = li['alt']
        url1 = '%s%s'%(g_url_prfix, img)
        if url1 not in urls:
            urls.append(url1)
            # print('img=', img, 'name=', name, 'url1=', url1)
            download(url1, name, index)

def main():
    createDir(g_save_path)
    for i in range(1, 64):
        global g_count
        g_count = 0
        url = g_url_begin + 'index_' + str(i) + '.html'
        imagespider(url, str(i))
        
if __name__ == "__main__":
    main()